let App = getApp();

Page({
  data: {
    opStr: "",
    result: '',
    mark: true,
    opArr: [],
    relArr: [],
    hid_val: '',
    hisArr: [],
    num: 0,
    kk: '',
    condition: '',
    is_show: true,
    use_time: 0,
    timer:''
  },
  onLoad: function (option) {
    var that = this;
    var kk = '';
    
    if (typeof option.pp != 'undefined') {

      kk = JSON.parse(option.pp);

      kk.step_val = kk.step;
      kk.op_str_val = kk.op_str.join(',');
      that.setData({
        'totalTime': kk.max_sec,
        'progressTime': kk.max_sec
      })
    }

    that.setData({
      'kk': kk
    })
    
    // this.loadConditionList(kk);
  },
  onShow: function () {

    this.loadConditionList(this.data.kk);
    this.setData({
      'result': '',
      'hisArr': [],
      
    })
    
    
  },
  //事件处理函数
  bindViewTap: function () {
    wx.showModal({
      title:'提示',
      content:'确定交卷吗',
      success:function(res){
        if(res.confirm){
          wx.switchTab({
            url: '/pages/index/index',
          })
        }
      }
    })
  },
  playTime:function(){
    let that=this
    this.setData({
      use_time:that.data.use_time+1
    })
  },

  evt_touch: function (e) {
    let that = this;
    
    //按下的按钮
    var num = e.target.id;

    //当前结果值
    var result = this.data.result + "" + num;
    that.setData({
      'result': result
    })
  },

  
  getdata: function (e) {
    var that = this;
    if (that.data.result == '') {
      wx.showToast({
        title: '请填写答案',
        duration: 2000,
        icon: 'error'
      })
      return false;
    }
    var arr = {
      op_str: that.data.opStr,
      val: that.data.result,
      hid_val: that.data.hid_val
    }
    that.data.hisArr.push(arr);

    if (that.data.num == that.data.hisArr.length) {
      that.setData({
        'is_show': false,
      })
      let u=that.data.use_time;
      wx.showToast({
        title: '答题完毕',
        duration: 2000,
        icon: 'loading'
      })
      setTimeout(function () {
        wx.redirectTo({
          url: '/pages/study/history?show_arr=' + JSON.stringify(that.data.hisArr) + "&use_time=" + u,
        })
      }, 2000)
      return false;

    }

    that.setData({
      'condition': that.data.hisArr.length + 1
    })
    
    that.setData({
      'opStr': that.data.opArr.splice(0, 1).toString(),
      'result': '',
      'hid_val': that.data.relArr.splice(0, 1).toString()
    })
  },
  show() {
    wx.hideLoading({
      success: (res) => {},
    })
  },
  //清空值
  empty_val() {
    this.setData({
      'result': ''
    })
  },
  //删除输入值
  clear_val() {
    
    var str = this.data.result;
    var newStr = str.substring(str.length - 1, -(str.length - 1))
    this.setData({
      'result': newStr
    })
  },
  //加载算式
  loadConditionList(aa) {
    var that = this;
    wx.showLoading({
      title: "正在生成",
    });
    App._get('study/math', aa, function (res) {

      if (res.code != 0) {
        wx.showToast({
          title: '加载算式出错',
          icon: 'none',
          image: '../../images/error.png',
          duration: 2000,
        })
        return false;
      }
      that.setData({
        'opArr': res.data.arr,
        'relArr': res.data.rel,
        'num': res.data.arr.length,
        'mark':true,
        'use_time':0
      });
      
      var str = that.data.opArr.splice(0, 1).toString();
      var val = that.data.relArr.splice(0, 1).toString();

      that.setData({
        'opStr': str,
        'hid_val': val,
        'condition': 1,
      })
      setInterval(that.playTime,1000);
    }, function () {
      wx.hideLoading({
        success: (res) => {
          
        },
      })
    }, function () {
      wx.hideLoading({
        success: (res) => {
          
        },
      })
    })


  }
})